package com.jhhc.StormSurgeForecast.AstronomicalTide.Util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jhhc.FloodForecast.Sensor.Pojo.BDMS_ST_SENSORALARM_B;
import com.jhhc.FloodForecast.Sensor.Pojo.BDMS_ST_SENSOR_B;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_SENSORALARM_BService;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_SENSOR_BService;
import com.jhhc.RHDB.Data.Pojo.Data;
import com.jhhc.RHDB.Data.Pojo.ST_TIDE_H;
import com.jhhc.RHDB.Data.Pojo.ST_TIDE_R;
import com.jhhc.RHDB.Data.Service.ST_TIDE_HService;
import com.jhhc.RHDB.Data.Service.ST_TIDE_RService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Dao.*;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.*;

import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.ChartEntity.ChartDataResult;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.DTO.*;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.DTO.HighAndLowTidesDatas;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_ST_ASTROTD_FService;
import com.jhhc.StormSurgeForecast.StormSurge.Dao.BDMS_ST_ASTROTD_FMapper;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.BDMS_ST_TIDE_E;
import com.jhhc.StormSurgeForecast.StormSurge.Service.BDMS_ST_TIDE_EService;
import com.jhhc.StormSurgeForecast.StormSurge.Uitl.ArithmeticUtil;
import com.jhhc.utils.ChartData;
import com.jhhc.utils.GetDataService;
import com.jhhc.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import  com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_FBC_STRMSRG_HARINFOService;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.io.File;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class GetTideUitl {

    @Autowired
    private BDMS_ST_SENSOR_BService bdms_st_sensor_bService;

    @Autowired
    private BDMS_ST_ASTROTD_FService bdms_st_astrotd_fService;

    @Autowired
    private GetDataService getDataService;
    @Autowired
    private ST_TIDE_RService st_tide_rService;
    @Autowired
    private ST_TIDE_HService st_tide_hService;
    @Autowired
    private GetInformTxtUtil getInformTxtUtil;
    @Autowired
    private ArithmeticUtil arithmeticUtil;
    @Autowired
    private BDMS_ST_TIDE_EService bdms_st_tide_eService;
    @Autowired
    private BDMS_FBC_STRMSRG_HARINFOService bdms_fbc_strmsrg_harinfoService;
    @Autowired
    private BDMS_ST_SENSORALARM_BService bdms_st_sensoralarm_bService;
    @Resource
    private BDMS_CALCULATESCHEME_M1Mapper bdms_calculatescheme_m1Mapper;
    @Resource
    private BDMS_FBC_RESULT_PMapper bdms_fbc_result_pMapper;
    @Autowired
    private BDMS_ST_ASTROTD_FMapper bdms_st_astrotd_fMapper;

    @Resource
    private  BDMS_ST_TIDE_EMapper bdms_st_tide_eMapper;
    @Resource
    private com.jhhc.StormSurgeForecast.AstronomicalTide.Dao.bdms_forecastsubscheme_bMapper bdms_forecastsubscheme_bMapper;
    @Resource
    private com.jhhc.StormSurgeForecast.AstronomicalTide.Dao.bdms_forecastparameters_bMapper bdms_forecastparameters_bMapper;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Value("${serverType}")
    String serverType;
    @Value("${spring.redis.host}")
    String redisUri;
    Calendar calendar = new GregorianCalendar();
    DecimalFormat df = new DecimalFormat("0.00");
    public List<Data> getDataListStream(List<ST_TIDE_R> st_tide_rs) {
        return st_tide_rs.stream()
                .map(stTideR -> {
                    Data data = new Data();
                    data.setTM(stTideR.getTm().substring(0, 19));
                    data.setValue(Double.valueOf(df.format(stTideR.getTdz())));
                    return data;
                })
                .collect(Collectors.toList());
    }
    public List<Data> compareDataSets(List<Data> dataSet1, List<Data> dataSet2) {
        List<Data> dataList = new ArrayList<>();
        // 创建两个Map对象，用于存储数据集
        Map<String, Data> dataMap1 = new HashMap<>();
        Map<String, Data> dataMap2 = new HashMap<>();

        // 遍历第一组数据集，将每个Data对象添加到第一个Map中
        for (Data data : dataSet1) {
            dataMap1.put(data.getTM(), data);
        }

        // 遍历第二组数据集
        for (Data data : dataSet2) {
            // 检查时间是否存在于第一个Map中
            if (dataMap1.containsKey(data.getTM())) {
                Data dataCom = new Data();
                Data data1 = dataMap1.get(data.getTM());
                dataCom.setTM(data.getTM());

                // 计算差值并进行处理
                double difference = data.getValue() - data1.getValue();
                dataCom.setValue(Double.valueOf(df.format(difference)));
                dataList.add(dataCom);
                // 在这里可以对差值进行进一步的处理，例如打印或保存结果

            }
        }
        return dataList;
    }

    //-获取实测水情图
    public List<ChartDataResult> getRealTimeHydrologic2(int Pid, List<String> StationCodes, String StartTime, String EndTime, int departmentId) {


        List<ChartDataResult> realTimeHydrologics = new ArrayList<>();
        //创建取数码集合
        List<String> datacCodes = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bdms_st_sensor_bService.selectBySTCD29(Pid, StationCodes);
        if (bdms_st_sensor_bs!=null&&bdms_st_sensor_bs.size()>0){
            for (BDMS_ST_SENSOR_B bdms_st_sensor_b : bdms_st_sensor_bs) {
                datacCodes.add(bdms_st_sensor_b.getDatacode());
            }
            List<ST_TIDE_R> st_tide_rList = st_tide_rService.selectByStcd(datacCodes, StartTime, EndTime);

            //获取取数码后去查询对应的信息
            //天文潮数据
            List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFList = bdms_st_astrotd_fService.selectFTDZ(StationCodes, StartTime, EndTime, departmentId);



            List<BDMS_ST_SENSORALARM_B> bdms_st_sensoralarm_bs = bdms_st_sensoralarm_bService.selectByStcd(StationCodes, "TDZ", Pid);
            for (String stcd:StationCodes) {
                ChartDataResult chartDataResult = new ChartDataResult();
                List<ChartData> chartDatas = new ArrayList<>();
                BDMS_ST_SENSOR_B bdms_st_sensor_b = bdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B -> BDMS_ST_SENSOR_B.getStcd().equals(stcd)).findAny().orElse(null);

                if (bdms_st_sensor_b != null) {
                    chartDataResult.setTitle(bdms_st_sensor_b.getStnm() + "(" + stcd + ")水情过程图");
                    chartDataResult.setStationName(bdms_st_sensor_b.getStnm());
                }
                BDMS_ST_SENSORALARM_B bdms_st_sensoralarm_b = bdms_st_sensoralarm_bs.stream().filter(BDMS_ST_SENSORALARM_Bx ->
                        BDMS_ST_SENSORALARM_Bx.getStcd().equals(stcd)
                ).findAny().orElse(null);

                if (bdms_st_sensoralarm_b != null) {
                    chartDataResult.setAlarmTideData(String.valueOf(bdms_st_sensoralarm_b.getAlarmvalue()));
                }
                chartDataResult.setSubTitle(StartTime + "--" + EndTime);
                chartDataResult.setStationCode(stcd);
                chartDataResult.setAxisX(StartTime + "," + EndTime);
                String y = "";
                String datacode = bdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B -> BDMS_ST_SENSOR_B.getStcd().equals(stcd)).findAny().orElse(null).getDatacode();
                List<ST_TIDE_R> st_tide_rs = st_tide_rList.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(datacode)).collect(Collectors.toList());
                List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs = bdmsStAstrotdFList.stream().filter(BDMS_ST_ASTROTD_F -> BDMS_ST_ASTROTD_F.getStcd().equals(stcd)).collect(Collectors.toList());

                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                List<Data> datas = new ArrayList<>();
                //解析天文潮位
                for (BDMS_ST_ASTROTD_F bdmsStAstrotdF:bdmsStAstrotdFs ){
                    Data data = new Data();
                    data.setTM(bdmsStAstrotdF.getYmdh().substring(0,19));
                    data.setValue(bdmsStAstrotdF.getFtdz());
                    datas.add(data);
                }
                Double max=null;
                Double min =null;
                String[] valueNames = {"实测潮位", "天文潮位","实测增水"};
                for (String valueName : valueNames) {
                    ChartData chartData = new ChartData();
                    chartData.setValueName(valueName);
                    chartData.setUnit("m");
                    List<Data> dataList;
                    switch (valueName) {
                        case "实测潮位":
                            chartData.setSensorType("tdz");
                            dataList = getDataListStream(st_tide_rs);
                            chartData.setGroupOrder(1);
                            chartData.setOrder(1);
                            if (!dataList.isEmpty()) {
                                chartData.setMaxData(dataList.stream()
                                        .max(Comparator.comparing(Data::getValue))
                                        .orElse(null));
                                chartData.setMinData(dataList.stream()
                                        .min(Comparator.comparing(Data::getValue))
                                        .orElse(null));
                                if (chartData.getMaxData() != null) {
                                    if (max == null) {
                                        max = chartData.getMaxData().getValue();
                                    } else {
                                        if (max < chartData.getMaxData().getValue()) {
                                            max = chartData.getMaxData().getValue();
                                        }
                                    }
                                }
                                if (chartData.getMinData() != null) {
                                    if (min == null) {
                                        min = chartData.getMinData().getValue();
                                    } else {
                                        if (min > chartData.getMinData().getValue()) {
                                            min = chartData.getMinData().getValue();
                                        }
                                    }
                                }
                                String y1 = null;
                                if (chartData.getMaxData() != null && chartData.getMinData() != null) {
                                    if (min < 0) {
                                        y1 = "-" + Math.round(Math.abs(min) + 0.5);
                                    } else {
                                        y1 = String.valueOf(Math.round(min));
                                    }
                                    y = y1 + "," + Math.round((max * 1.5));
                                }
                                chartData.setDatas(dataList);
                                chartDataResult.setAxisY(y);
                                chartDatas.add(chartData);
                            }


                            break;
                        case "天文潮位":
                            chartData.setSensorType("twc");
                            dataList = datas;
                            if (!dataList.isEmpty()) {
                                chartData.setMaxData(dataList.stream()
                                        .max(Comparator.comparing(Data::getValue))
                                        .orElse(null));
                                chartData.setMinData(dataList.stream()
                                        .min(Comparator.comparing(Data::getValue))
                                        .orElse(null));
                                if (chartData.getMaxData() != null) {
                                    if (max == null) {
                                        max = chartData.getMaxData().getValue();
                                    } else {
                                        if (max < chartData.getMaxData().getValue()) {
                                            max = chartData.getMaxData().getValue();
                                        }
                                    }
                                }
                                if (chartData.getMinData() != null) {
                                    if (min == null) {
                                        min = chartData.getMinData().getValue();
                                    } else {
                                        if (min > chartData.getMinData().getValue()) {
                                            min = chartData.getMinData().getValue();
                                        }
                                    }
                                }
                                String y1 = null;
                                if (chartData.getMaxData() != null && chartData.getMinData() != null) {
                                    if (min < 0) {
                                        y1 = "-" + Math.round(Math.abs(min) + 0.5);
                                    } else {
                                        y1 = String.valueOf(Math.round(min));
                                    }
                                    y = y1 + "," + Math.round((max * 1.5));
                                }
                                chartData.setGroupOrder(2);
                                chartData.setOrder(1);
                                chartData.setDatas(dataList);
                                chartDataResult.setAxisY(y);
                                chartDatas.add(chartData);
                            }

                            break;
                        case "实测增水":

                            //实测增水
                            if (chartDatas.size()==2) {
                                if (chartDatas.get(0).getDatas() != null && chartDatas.get(1).getDatas() != null) {
                                    dataList = compareDataSets(chartDatas.get(1).getDatas(), chartDatas.get(0).getDatas());
                                    chartData.setMaxData(dataList.stream()
                                            .max(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setMinData(dataList.stream()
                                            .min(Comparator.comparing(Data::getValue))
                                            .orElse(null));
                                    chartData.setAverage(Double.valueOf(df.format(dataList.stream().collect(Collectors.averagingDouble(Data::getValue)))));
                                    chartData.setDatas(dataList);
                                    chartData.setGroupOrder(3);
                                    chartData.setOrder(1);
                                    chartDatas.add(chartData);
                                    chartData.setSensorType("zs");
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
                chartDatas = chartDatas.stream().filter(chartData -> chartData.getDatas().size()>0).collect(Collectors.toList());
                System.out.println(chartDatas);
                chartDatas.sort(Comparator.comparing(ChartData::getGroupOrder)
                        .thenComparing(ChartData::getOrder));


                chartDataResult.setResults(chartDatas);
                realTimeHydrologics.add(chartDataResult);
            }
        }

        return realTimeHydrologics;
    }


    //-获取实测水情图
    public List<RealTimeHydrologic> getRealTimeHydrologic(int Pid, List<String> StationCodes, String StartTime, String EndTime, int departmentId) {
        List<RealTimeHydrologic> realTimeHydrologics = new ArrayList<>();
        //创建取数码集合
        List<String> datacCodes = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bdms_st_sensor_bService.selectRealTimeHydrologic29(Pid, StationCodes);
        if (bdms_st_sensor_bs!=null&&bdms_st_sensor_bs.size()>0){
            for (BDMS_ST_SENSOR_B bdms_st_sensor_b : bdms_st_sensor_bs) {
                datacCodes.add(bdms_st_sensor_b.getDatacode());
            }
            List<ST_TIDE_R> st_tide_rList = st_tide_rService.selectByStationArrys(datacCodes, StartTime, EndTime);
            //获取取数码后去查询对应的信息
            //天文潮数据
            List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFList = bdms_st_astrotd_fService.selectFTDZ(StationCodes, StartTime, EndTime, departmentId);
            //List<BDMS_ST_ASTROTD_F> bdms_st_astrotd_fList = bdms_st_astrotd_fService.selectNewEstData(StationCodes);
            List<ST_TIDE_R> stTideRs =null;
            if (st_tide_rList.size()==0){
                stTideRs = st_tide_rService.selectNewEstData(datacCodes);
                System.out.println(stTideRs);
            }
            for (String stcd:StationCodes){
                RealTimeHydrologic realTimeHydrologic = new RealTimeHydrologic();
                List<RealTimeHydrologicData> realTimeHydrologicData = new ArrayList<>();
                realTimeHydrologic.setStationCode(stcd);
                BDMS_ST_SENSOR_B   bdms_st_sensor_b =  bdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B->BDMS_ST_SENSOR_B.getStcd().equals(stcd)).findAny().orElse(null);

                List<ST_TIDE_R> st_tide_rs = st_tide_rList.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(bdms_st_sensor_b.getDatacode())).collect(Collectors.toList());

                List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs =bdmsStAstrotdFList.stream().filter(BDMS_ST_ASTROTD_F -> BDMS_ST_ASTROTD_F.getStcd().equals(stcd)).collect(Collectors.toList());

                List<ST_TIDE_R> st_tide_rs1 = st_tide_rs.stream().filter(ST_TIDE_R -> "00".equals(ST_TIDE_R.getTm().substring(14,16))).collect(Collectors.toList());

                if (bdmsStAstrotdFs.size()>0){
                    for (BDMS_ST_ASTROTD_F bdmsStAstrotdF:bdmsStAstrotdFs){
                        String FtdzTime = bdmsStAstrotdF.getYmdh();
                        ST_TIDE_R tied= st_tide_rs.stream().filter(st_tide_r -> st_tide_r.getTm().substring(0,19).equals(FtdzTime.substring(0,19))).findAny().orElse(null);
                        RealTimeHydrologicData realTimeHydrologicDataS = new RealTimeHydrologicData();
                        realTimeHydrologicDataS.setTime(FtdzTime);
                        if (tied!=null){
                            realTimeHydrologicDataS.setTdz(tied.getTdz());
                            realTimeHydrologicDataS.setStrmsrg(Double.parseDouble(String.format("%.4f", tied.getTdz()-bdmsStAstrotdF.getFtdz())));
                        }
                        realTimeHydrologicDataS.setFtdz(bdmsStAstrotdF.getFtdz());

                        realTimeHydrologicData.add(realTimeHydrologicDataS);
                    }
                }else {
                    for (ST_TIDE_R bdmsStAstrotdF:st_tide_rs1){
                        String FtdzTime = bdmsStAstrotdF.getTm();
                        BDMS_ST_ASTROTD_F tied= bdmsStAstrotdFs.stream().filter(st_tide_r -> st_tide_r.getYmdh().substring(0,19).equals(FtdzTime.substring(0,19))).findAny().orElse(null);
                        RealTimeHydrologicData realTimeHydrologicDataS = new RealTimeHydrologicData();
                        realTimeHydrologicDataS.setTime(FtdzTime);
                        if (tied!=null){
                            realTimeHydrologicDataS.setFtdz(tied.getFtdz());
                            realTimeHydrologicDataS.setStrmsrg(Double.parseDouble(String.format("%.4f",bdmsStAstrotdF.getTdz()- tied.getFtdz())));
                        }
                        realTimeHydrologicDataS.setTdz(bdmsStAstrotdF.getTdz());

                        realTimeHydrologicData.add(realTimeHydrologicDataS);
                    }
                }


                realTimeHydrologic.setDatas(realTimeHydrologicData);
                if (st_tide_rList.size()==0){
                    String datacode= bdms_st_sensor_bs.stream().filter(st_sensor_b -> st_sensor_b.getStcd().equals(stcd)).findAny().get().getDatacode();
                    ST_TIDE_R st_tide_r= stTideRs.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(datacode)).findAny().orElse(null);
                    if (st_tide_r!=null){
                        String endTime=st_tide_r.getTm();
                        try {
                            Date end =  format.parse(endTime);
                            calendar.setTime(end);
                            calendar.add(Calendar.DATE, -3);
                        }catch (Exception ignored){

                        }
                        realTimeHydrologic.setDataStartTime(format.format(calendar.getTime()));
                        realTimeHydrologic.setDataEndTime(endTime);
                    }


                }
                realTimeHydrologics.add(realTimeHydrologic);
            }
        }


        System.out.println(realTimeHydrologics);
        return realTimeHydrologics;
    }


    //天文潮数据
    public  List<ForecastTide> getForecastTide(List<String> stationCodes,String startTime,String endTime){
        List<ForecastTide> forecastTides = new ArrayList<>();
        //获取取数码
        List<String> datacCodes = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bdms_st_sensor_bService.selectTwcByStcds(stationCodes);
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b:bdms_st_sensor_bs){
            datacCodes.add(bdms_st_sensor_b.getDatacode());
        }
        List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs = bdms_st_astrotd_fService.selectFTDZ(datacCodes,startTime,endTime,0);
        Map<String, List<BDMS_ST_ASTROTD_F>> StcdListGroup = bdmsStAstrotdFs.stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getStcd, Collectors.toList()));
        for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> stcdList : StcdListGroup.entrySet()) {
            ForecastTide forecastTide = new ForecastTide();
            forecastTide.setStationCode(stcdList.getKey());
            List<Data> datas = new ArrayList<>();
            for (BDMS_ST_ASTROTD_F bdms_st_astrotd_f:stcdList.getValue()){
                Data data =  new Data();
                data.setTM(bdms_st_astrotd_f.getYmdh().substring(0,19));
                data.setValue(bdms_st_astrotd_f.getFtdz());
                datas.add(data);
            }
            forecastTide.setDataList(datas);
            forecastTides.add(forecastTide);
        }
        return  forecastTides;
    }

    //精度评定
    public List<AccuracyEvaluationResult>  getAccuracyEvaluation(Integer projectId, String startTime, String endTime,boolean collateData){
        //返回对象
        List<AccuracyEvaluationResult> accuracyEvaluationResults = new ArrayList<>();
        List<String> stationCodes = new ArrayList<>();
        List<BDMS_ST_ASTROTD_F> gdcTides = new ArrayList<>();
        //

        //获取pid 下所有的站码
        List<BDMS_ST_SENSOR_B> pidbdms_st_sensor_bs = bdms_st_sensor_bService.queryDisnetStcd(projectId);
        //获取天文潮 取数码
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b:pidbdms_st_sensor_bs){
            if (bdms_st_sensor_b.getSensororder()==1){
                stationCodes.add(bdms_st_sensor_b.getStcd());
            }
        }
        //获取天文潮取数码
        List<String> twcDatacCodes = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> twcbdms_st_sensor_bs = bdms_st_sensor_bService.selectTwcByStcds(stationCodes);
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b:twcbdms_st_sensor_bs){
            twcDatacCodes.add(bdms_st_sensor_b.getDatacode());
        }
        //获取实测取数码
        List<String> tdzDatacCodes = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bdms_st_sensor_bService.selectByStcds(stationCodes);
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b:bdms_st_sensor_bs){
            tdzDatacCodes.add(bdms_st_sensor_b.getDatacode());
        }
        //天文潮数据
        List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs = bdms_st_astrotd_fService.selectFTDZ(twcDatacCodes,startTime,endTime,0);

        List<ST_TIDE_H> st_tide_hs =null;
        List<ST_TIDE_R> st_tide_rList = new ArrayList<>();
        //实测数据
        if (collateData){
            st_tide_hs =st_tide_hService.queryByStationCodes(tdzDatacCodes, startTime, endTime);
            if (st_tide_hs!=null&&st_tide_hs.size()>0){
                for (ST_TIDE_H st_tide_h:st_tide_hs){
                    ST_TIDE_R st_tide_r = new ST_TIDE_R();
                    st_tide_r.setStcd(st_tide_h.getStcd());
                    st_tide_r.setTm(st_tide_h.getTm());
                    if (st_tide_h.getHltdmk()!=null&& "".equals(st_tide_h.getHltdmk())){
                        st_tide_r.setHltdmk(Integer.valueOf(st_tide_h.getHltdmk()));
                    }
                    st_tide_r.setTdz(st_tide_h.getTdz());
                    st_tide_r.setAirp(st_tide_h.getAirp());
                    st_tide_r.setTdchrcd(st_tide_h.getTdchrcd());
                    st_tide_r.setTdptn(st_tide_r.getTdptn());
                    st_tide_rList.add(st_tide_r);
                }
            }
        }else {
            st_tide_rList= st_tide_rService.selectByStcd(tdzDatacCodes, startTime, endTime);
        }
        Map<String, List<BDMS_ST_ASTROTD_F>> StcdListGroup = bdmsStAstrotdFs.stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getStcd, Collectors.toList()));
        for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> stcdList : StcdListGroup.entrySet()) {




            int zero =0;
            int zero_t =0;
            int ten = 0;
            int twenty = 0;
            int thirty = 0;
            int forty = 0;
            int fifty = 0;
            int ten_t = 0;
            int twenty_t = 0;
            int thirty_t = 0;
            int forty_t = 0;
            int fifty_t= 0;
            //单个返回对象
            AccuracyEvaluationResult accuracy = new AccuracyEvaluationResult();
            accuracy.setStationCode(stcdList.getKey());
            String stnm =twcbdms_st_sensor_bs.stream().filter(bdms_st_sensor_b->bdms_st_sensor_b.getStcd().equals(stcdList.getKey())).findAny().get().getStnm();
            accuracy.setStationName(stnm);
            List<AccuracyEvaluation> accuracyEvaluations = new ArrayList<>();
            //10cm
            AccuracyEvaluation zeroAccuracyEvaluation = new AccuracyEvaluation();
            AccuracyEvaluation tenAccuracyEvaluation = new AccuracyEvaluation();
            AccuracyEvaluation twentyAccuracyEvaluation = new AccuracyEvaluation();
            AccuracyEvaluation thirtyAccuracyEvaluation = new AccuracyEvaluation();
            AccuracyEvaluation fortyAccuracyEvaluation = new AccuracyEvaluation();
            AccuracyEvaluation fiftyAccuracyEvaluation = new AccuracyEvaluation();
            //天文潮站码
            String TWCSTCD =    twcbdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B->BDMS_ST_SENSOR_B.getDatacode().equals(stcdList.getKey())).findAny().get().getStcd();
            //根据天文潮站码获取实测取数码
            String TDZSTCD =  bdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B->BDMS_ST_SENSOR_B.getStcd().equals(TWCSTCD)).findAny().get().getDatacode();
            //获取实测数据

            List<ST_TIDE_R> st_tide_rs = st_tide_rList.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(TDZSTCD)).collect(Collectors.toList());
            int res =st_tide_rs.size();
            //实测高低潮数据
            List <ST_TIDE_R> gdctide_rs =  st_tide_rs.stream().filter(st_tide_r ->st_tide_r.getHltdmk()!=null &&st_tide_r.getHltdmk()>0).collect(Collectors.toList());
            //循环数据
            for (BDMS_ST_ASTROTD_F bdms_st_astrotd_f:stcdList.getValue()){
                //以天筛选 获取对应高低潮
                String FtdzTime = bdms_st_astrotd_f.getYmdh();
                ST_TIDE_R tied= st_tide_rs.stream().filter(st_tide_r -> st_tide_r.getTm().substring(0,19).equals(FtdzTime.substring(0,19))).findAny().orElse(null);
                if (tied!=null&&tied.getTdz()!=null){
                    double zs =0.0;
                    if (tied.getTdz()<0&&bdms_st_astrotd_f.getFtdz()<0){
                        zs =(Math.abs(tied.getTdz()) -Math.abs(bdms_st_astrotd_f.getFtdz()))* 100;
                    }else {
                        zs = (tied.getTdz() - bdms_st_astrotd_f.getFtdz())* 100;
                    }
                    System.out.println(zs);
                   //double zs =(Double.parseDouble(String.format("%.2f", tied.getTdz()-bdms_st_astrotd_f.getFtdz())))*100;

                    if (zs==0){
                        zero++;
                    }else if (Math.abs(zs)<=10){
                        ten ++;
                   }else  if (Math.abs(zs)<=20){
                        twenty ++;
                   }else  if (Math.abs(zs)<=30){
                        thirty++;
                   }else  if (Math.abs(zs)<=40){
                        forty ++;
                   }else  if (Math.abs(zs)<=50){
                        fifty ++;
                   }
                }else{
                    if (bdms_st_astrotd_f.getHltdmk()!=null&&bdms_st_astrotd_f.getHltdmk()>0){
                        gdcTides.add(bdms_st_astrotd_f);
                    }

                }
            }
            //循环天文高低潮
            for (BDMS_ST_ASTROTD_F bdms_st_astrotd_f:gdcTides){
                String tiem =  bdms_st_astrotd_f.getYmdh().substring(0,10);
                ST_TIDE_R tide_r =  gdctide_rs.stream().filter(st_tide_r -> st_tide_r.getTm().substring(0,10).equals(tiem)&& st_tide_r.getHltdmk().equals(bdms_st_astrotd_f.getHltdmk())).findAny().orElse(null);
                if (tide_r!=null){
                    double zs =(Double.parseDouble(String.format("%.2f", tide_r.getTdz()-bdms_st_astrotd_f.getFtdz())))*100;
                    if (zs==0){
                        zero_t ++;
                    }else if (Math.abs(zs)<=10){
                        ten_t ++;
                    }else  if (Math.abs(zs)<=20){
                        twenty_t ++;
                    }else  if (Math.abs(zs)<=30){
                        thirty_t++;
                    }else  if (Math.abs(zs)<=40){
                        forty_t ++;
                    }else  if (Math.abs(zs)<=50){
                        fifty_t ++;
                    }
                }
            }
            //值
            //0cm
            zeroAccuracyEvaluation.setStandardRange("±0cm");
            //过程
            double zd =(double) zero/res;
            Statistics zeroProcessStatistics = new Statistics();
            zeroProcessStatistics.setQualifiedNumber(String.valueOf(zero));
            zeroProcessStatistics.setQualifiedRate(Math.round(zd*100)+"%");
            zeroAccuracyEvaluation.setProcessResult(zeroProcessStatistics);
            //高低潮
            double zdHl =(double) (zero_t)/res;
            Statistics zeroHlStatistics = new Statistics();
            zeroHlStatistics.setQualifiedNumber(String.valueOf(zero_t));
            zeroHlStatistics.setQualifiedRate(Math.round(zdHl*100)+"%");

            zeroAccuracyEvaluation.setHlResult(zeroHlStatistics);
            //总
            double zdAll =(double) (zero+zero_t)/res;
            Statistics zeroAllStatistics = new Statistics();
            zeroAllStatistics.setQualifiedNumber(String.valueOf(zero+zero_t));
            zeroAllStatistics.setQualifiedRate(Math.round(zdAll*100)+"%");
            zeroAccuracyEvaluation.setAllResult(zeroAllStatistics);
            accuracyEvaluations.add(zeroAccuracyEvaluation);
            //10cm
            tenAccuracyEvaluation.setStandardRange("≤±10cm");
            double d1 =(double) (zero+zero_t+ten+ten_t)/res;
            //过程
            double tenzd =(double) (zero+ten)/res;
            Statistics tenProcessStatistics = new Statistics();
            tenProcessStatistics.setQualifiedNumber(String.valueOf(ten+zero));
            tenProcessStatistics.setQualifiedRate(Math.round(tenzd*100)+"%");
            tenAccuracyEvaluation.setProcessResult(tenProcessStatistics);
            //高低潮
            double tenzdHl =(double) (ten_t+zero_t)/res;
            Statistics tenHlStatistics = new Statistics();
            tenHlStatistics.setQualifiedNumber(String.valueOf(ten_t+zero_t));
            tenHlStatistics.setQualifiedRate(Math.round(tenzdHl*100)+"%");
            tenAccuracyEvaluation.setHlResult(tenHlStatistics);

            //总
            Statistics tenAllStatistics = new Statistics();
            tenAllStatistics.setQualifiedNumber(String.valueOf((zero+zero_t+ten+ten_t)));
            tenAllStatistics.setQualifiedRate(Math.round(d1*100)+"%");
            tenAccuracyEvaluation.setAllResult(tenAllStatistics);
            accuracyEvaluations.add(tenAccuracyEvaluation);

            //20cm
            twentyAccuracyEvaluation.setStandardRange("≤±20cm");
            double d2 =(double) (zero+zero_t+ten+ten_t+twenty_t+twenty)/res;
            //过程
            double twzd =(double) (twenty+ten+zero)/res;
            Statistics twProcessStatistics = new Statistics();
            twProcessStatistics.setQualifiedNumber(String.valueOf(twenty+ten+zero));
            twProcessStatistics.setQualifiedRate(Math.round(twzd*100)+"%");
            twentyAccuracyEvaluation.setProcessResult(twProcessStatistics);
            //高低潮
            double twzdHl =(double) (twenty_t+ten_t+zero_t)/res;
            Statistics twHlStatistics = new Statistics();
            twHlStatistics.setQualifiedNumber(String.valueOf(twenty_t+ten_t+zero_t));
            twHlStatistics.setQualifiedRate(Math.round(twzdHl*100)+"%");
            twentyAccuracyEvaluation.setHlResult(twHlStatistics);

            //总
            Statistics twAllStatistics = new Statistics();
            twAllStatistics.setQualifiedNumber(String.valueOf((zero+zero_t+ten+ten_t+twenty_t+twenty)));
            twAllStatistics.setQualifiedRate(Math.round(d2*100)+"%");
            twentyAccuracyEvaluation.setAllResult(twAllStatistics);
            accuracyEvaluations.add(twentyAccuracyEvaluation);
            //30cm
            thirtyAccuracyEvaluation.setStandardRange("≤±30cm");
            double d3 =(double) (zero+zero_t+ten+ten_t+twenty_t+twenty+thirty+thirty_t)/res;
            //过程
            double thzd =(double) (zero+ten+twenty+thirty)/res;
            Statistics thProcessStatistics = new Statistics();
            thProcessStatistics.setQualifiedNumber(String.valueOf(zero+ten+twenty+thirty));
            thProcessStatistics.setQualifiedRate(Math.round(thzd*100)+"%");
            thirtyAccuracyEvaluation.setProcessResult(thProcessStatistics);
            //高低潮
            double thzdHl =(double) (thirty_t+twenty_t+ten_t+zero_t)/res;
            Statistics thHlStatistics = new Statistics();
            thHlStatistics.setQualifiedNumber(String.valueOf(thirty_t+twenty_t+ten_t+zero_t));
            thHlStatistics.setQualifiedRate(Math.round(thzdHl*100)+"%");
            thirtyAccuracyEvaluation.setHlResult(thHlStatistics);

            //总
            Statistics thAllStatistics = new Statistics();
            thAllStatistics.setQualifiedNumber(String.valueOf(zero+zero_t+ten+ten_t+twenty_t+twenty+thirty+thirty_t));
            thAllStatistics.setQualifiedRate(Math.round(d3*100)+"%");
            thirtyAccuracyEvaluation.setAllResult(thAllStatistics);
            accuracyEvaluations.add(thirtyAccuracyEvaluation);
            //40cm
            fortyAccuracyEvaluation.setStandardRange("≤±40cm");
            double d4 =(double) (zero+ten+ten_t+twenty_t+twenty+thirty+thirty_t+forty+forty_t)/res ;
            //过程
            double fozd =(double) (zero+ten+twenty+thirty+forty)/res;
            Statistics foProcessStatistics = new Statistics();
            foProcessStatistics.setQualifiedNumber(String.valueOf(zero+ten+twenty+thirty+forty));
            foProcessStatistics.setQualifiedRate(Math.round(fozd*100)+"%");
            fortyAccuracyEvaluation.setProcessResult(foProcessStatistics);

            //高低潮
            double fozdHl =(double) (thirty_t+twenty_t+ten_t+zero_t+forty_t)/res;
            Statistics foHlStatistics = new Statistics();
            foHlStatistics.setQualifiedNumber(String.valueOf(thirty_t+twenty_t+ten_t+zero_t+forty_t));
            foHlStatistics.setQualifiedRate(Math.round(fozdHl*100)+"%");
            fortyAccuracyEvaluation.setHlResult(foHlStatistics);

            //总
            Statistics foAllStatistics = new Statistics();
            foAllStatistics.setQualifiedNumber(String.valueOf(zero+ten+ten_t+twenty_t+twenty+thirty+thirty_t+forty+forty_t));
            foAllStatistics.setQualifiedRate(Math.round(d4*100)+"%");
            fortyAccuracyEvaluation.setAllResult(foAllStatistics);
            accuracyEvaluations.add(fortyAccuracyEvaluation);
            //50cm
            fiftyAccuracyEvaluation.setStandardRange("≤±50cm");
            double d5 =(double) (zero+ten+ten_t+twenty_t+twenty+thirty+thirty_t+forty+forty_t+fifty+fifty_t)/res;
            //过程
            double fizd =(double) (zero+ten+twenty+thirty+forty+fifty)/res;
            Statistics fiProcessStatistics = new Statistics();
            fiProcessStatistics.setQualifiedNumber(String.valueOf(zero+ten+twenty+thirty+forty+fifty));
            fiProcessStatistics.setQualifiedRate(Math.round(fizd*100)+"%");
            fiftyAccuracyEvaluation.setProcessResult(fiProcessStatistics);
            //高低潮
            double fizdHl =(double) (thirty_t+twenty_t+ten_t+zero_t+forty_t+fifty_t)/res;
            Statistics fiHlStatistics = new Statistics();
            fiHlStatistics.setQualifiedNumber(String.valueOf(thirty_t+twenty_t+ten_t+zero_t+forty_t));
            fiHlStatistics.setQualifiedRate(Math.round(fizdHl*100)+"%");
            fiftyAccuracyEvaluation.setHlResult(fiHlStatistics);
            //总
            Statistics fiAllStatistics = new Statistics();
            fiAllStatistics.setQualifiedNumber(String.valueOf(zero+ten+ten_t+twenty_t+twenty+thirty+thirty_t+forty+forty_t+fifty+fifty_t));
            fiAllStatistics.setQualifiedRate(Math.round(d5*100)+"%");
            fiftyAccuracyEvaluation.setAllResult(fiAllStatistics);
            accuracyEvaluations.add(fiftyAccuracyEvaluation);
            //System.out.println(res);
            accuracy.setAccuracyEvaluations(accuracyEvaluations);
            accuracyEvaluationResults.add(accuracy);
            //break;
        }
        return accuracyEvaluationResults;
    }

    //获取增水统计
    public List<AnstauStatisticsResult> getAnstauStatistics(List<String> stationCodes, String startTime, String endTime,int projectId,int deptId){
        List<AnstauStatisticsResult> anstauStatisticsResults = new ArrayList<>();
        //获取天文潮取数码
        List<String> twcDatacCodes = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> twcbdms_st_sensor_bs = bdms_st_sensor_bService.selectTwcByStcds(stationCodes);
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b:twcbdms_st_sensor_bs){
            twcDatacCodes.add(bdms_st_sensor_b.getDatacode());
        }
        //获取实测取数码
        List<String> tdzDatacCodes = new ArrayList<>();
        List<BDMS_ST_SENSOR_B> bdms_st_sensor_bs = bdms_st_sensor_bService.selectByStcds(stationCodes);
        for (BDMS_ST_SENSOR_B bdms_st_sensor_b:bdms_st_sensor_bs){
            tdzDatacCodes.add(bdms_st_sensor_b.getDatacode());
        }
        //天文潮数据
        List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs = bdms_st_astrotd_fService.selectFTDZ(twcDatacCodes,startTime,endTime,0);
        int res =bdmsStAstrotdFs.size();
        //实测数据
        List<ST_TIDE_R> st_tide_rList = st_tide_rService.selectByStcd(tdzDatacCodes, startTime, endTime);


        //判断实测数据是否为空 为空返回有数据的时间
        if (st_tide_rList.size()==0){
            AnstauStatisticsResult anstauStatisticsResult = new AnstauStatisticsResult();
            anstauStatisticsResult.setStationCode(tdzDatacCodes.get(0));
            List<ST_TIDE_R> stTideRs =null;
            stTideRs = st_tide_rService.selectNewEstData(tdzDatacCodes);
            if (stTideRs.size()>0){
                ST_TIDE_R st_tide_r= stTideRs.get(0);
                String endTime1=st_tide_r.getTm();
                try {
                    Date end =  format.parse(endTime1);
                    calendar.setTime(end);
                    calendar.add(Calendar.DATE, -3);
                }catch (Exception ignored){

                }
                anstauStatisticsResult.setDataStartTime(format.format(calendar.getTime()));
                anstauStatisticsResult.setDataEndTime(endTime1);
            }else {
                if (bdmsStAstrotdFs.size()>0){
                    Map<String, List<BDMS_ST_ASTROTD_F>> StcdListGroup = bdmsStAstrotdFs.stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getStcd, Collectors.toList()));
                    for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> stcdList : StcdListGroup.entrySet()) {
                        //单个返回对象
                        anstauStatisticsResult.setStationCode(stcdList.getKey());
                        String stnm =twcbdms_st_sensor_bs.stream().filter(bdms_st_sensor_b->bdms_st_sensor_b.getStcd().equals(stcdList.getKey())).findAny().get().getStnm();
                        anstauStatisticsResult.setStationName(stnm);
                        List<AnstauStatistics> anstauStatisticsList = new ArrayList<>();
                        //数据根据天分组
                        Map<String, List<BDMS_ST_ASTROTD_F>> DayListGroup = stcdList.getValue().stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getDay, Collectors.toList()));
                        for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> dayList : DayListGroup.entrySet()) {
                            AnstauStatistics anstauStatistics = new AnstauStatistics();
                            anstauStatistics.setDay(dayList.getKey());
                            //天文潮
                            List<TidalMonthlyData> twc_lsit = new ArrayList<>();
                            List<HighAndLowTidesDatas> twcgdc_lsit = new ArrayList<>();
                            //循环数据
                            Map<String, List<BDMS_ST_ASTROTD_F>> HourListGroup = dayList.getValue().stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getHour, Collectors.toList()));
                            for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> hourList : HourListGroup.entrySet()) {
                                TidalMonthlyData tdzTidalMonthlyData = new TidalMonthlyData();
                                TidalMonthlyData twcTidalMonthlyData = new TidalMonthlyData();
                                TidalMonthlyData zsTidalMonthlyData = new TidalMonthlyData();
                                tdzTidalMonthlyData.setHour(hourList.getKey());
                                twcTidalMonthlyData.setHour(hourList.getKey());
                                zsTidalMonthlyData.setHour(hourList.getKey());
                                List<Data> dataList = new ArrayList<>();
                                List<Data> zsdataList = new ArrayList<>();
                                List<Data> tdzdataList = new ArrayList<>();
                                for (BDMS_ST_ASTROTD_F bdms_st_astrotd_f:hourList.getValue()){
                                    if (bdms_st_astrotd_f.getHltdmk()==0){
                                        Data data = new Data();
                                        data.setTM(bdms_st_astrotd_f.getYmdh());
                                        data.setValue(bdms_st_astrotd_f.getFtdz());
                                        dataList.add(data);
                                        dataList.sort(Comparator.comparing(Data::getTM));
                                    }else {
                                        HighAndLowTidesDatas twcHighAndLowTidesDatas = new HighAndLowTidesDatas();
                                        twcHighAndLowTidesDatas.setValue(bdms_st_astrotd_f.getFtdz());
                                        twcHighAndLowTidesDatas.setTm(bdms_st_astrotd_f.getYmdh());
                                        twcHighAndLowTidesDatas.setHltdmk(bdms_st_astrotd_f.getHltdmk());
                                        twcgdc_lsit.add(twcHighAndLowTidesDatas);
                                    }
                                }
                                tdzTidalMonthlyData.setDatas(tdzdataList);
                                twcTidalMonthlyData.setDatas(dataList);
                                zsTidalMonthlyData.setDatas(zsdataList);
                                twc_lsit.add(twcTidalMonthlyData);
                                twc_lsit.sort(Comparator.comparing(TidalMonthlyData::getHour));
                            }
                            anstauStatistics.setTwcHLs(twcgdc_lsit);
                            anstauStatistics.setTwcs(twc_lsit);
                            anstauStatisticsList.add(anstauStatistics);
                        }
                        anstauStatisticsList.sort(Comparator.comparing(AnstauStatistics::getDay));
                        anstauStatisticsResult.setDatas(anstauStatisticsList);
                    }
                }
            }
            anstauStatisticsResults.add(anstauStatisticsResult);
        }else { //站码分组循环
            if (bdmsStAstrotdFs.size()>0){
                Map<String, List<BDMS_ST_ASTROTD_F>> StcdListGroup = bdmsStAstrotdFs.stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> stcdList : StcdListGroup.entrySet()) {
                    //单个返回对象
                    AnstauStatisticsResult anstauStatisticsResult = new AnstauStatisticsResult();
                    anstauStatisticsResult.setStationCode(stcdList.getKey());
                    String stnm =twcbdms_st_sensor_bs.stream().filter(bdms_st_sensor_b->bdms_st_sensor_b.getStcd().equals(stcdList.getKey())).findAny().get().getStnm();
                    anstauStatisticsResult.setStationName(stnm);
                    List<AnstauStatistics> anstauStatisticsList = new ArrayList<>();

                    //天文潮站码
                    String TWCSTCD =    twcbdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B->BDMS_ST_SENSOR_B.getDatacode().equals(stcdList.getKey())).findAny().get().getStcd();
                    //根据天文潮站码获取实测取数码
                    String TDZSTCD =  bdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B->BDMS_ST_SENSOR_B.getStcd().equals(TWCSTCD)).findAny().get().getDatacode();
                    //获取实测数据
                    List<ST_TIDE_R> st_tide_rs = st_tide_rList.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(TDZSTCD)).collect(Collectors.toList());
                    //实测高低潮数据
                    List <ST_TIDE_R> gdctide_rs =  st_tide_rs.stream().filter(st_tide_r ->st_tide_r.getHltdmk()!=null &&st_tide_r.getHltdmk()>0).collect(Collectors.toList());
                    //数据根据天分组
                    Map<String, List<BDMS_ST_ASTROTD_F>> DayListGroup = stcdList.getValue().stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getDay, Collectors.toList()));
                    for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> dayList : DayListGroup.entrySet()) {
                        AnstauStatistics anstauStatistics = new AnstauStatistics();
                        anstauStatistics.setDay(dayList.getKey());
                        //实测
                        List<TidalMonthlyData> tdz_lsit = new ArrayList<>();
                        List<HighAndLowTidesDatas> tdzgdc_lsit = new ArrayList<>();
                        //天文潮
                        List<TidalMonthlyData> twc_lsit = new ArrayList<>();
                        List<HighAndLowTidesDatas> twcgdc_lsit = new ArrayList<>();
                        //zs
                        List<TidalMonthlyData> zs_lsit = new ArrayList<>();
                        List<HighAndLowTidesDatas> zsgdc_lsit = new ArrayList<>();
                        //循环数据
                        Map<String, List<BDMS_ST_ASTROTD_F>> HourListGroup = dayList.getValue().stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getHour, Collectors.toList()));
                        for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> hourList : HourListGroup.entrySet()) {
                            TidalMonthlyData tdzTidalMonthlyData = new TidalMonthlyData();
                            TidalMonthlyData twcTidalMonthlyData = new TidalMonthlyData();
                            TidalMonthlyData zsTidalMonthlyData = new TidalMonthlyData();

                            tdzTidalMonthlyData.setHour(hourList.getKey());
                            twcTidalMonthlyData.setHour(hourList.getKey());
                            zsTidalMonthlyData.setHour(hourList.getKey());
                            List<Data> dataList = new ArrayList<>();
                            List<Data> zsdataList = new ArrayList<>();
                            List<Data> tdzdataList = new ArrayList<>();
                            for (BDMS_ST_ASTROTD_F bdms_st_astrotd_f:hourList.getValue()){
                                ST_TIDE_R tied= st_tide_rs.stream().filter(st_tide_r -> st_tide_r.getTm().substring(0,19).equals(bdms_st_astrotd_f.getYmdh().substring(0,19))).findAny().orElse(null);
                                if (tied!=null){
                                    double zs =(Double.parseDouble(String.format("%.2f", tied.getTdz()-bdms_st_astrotd_f.getFtdz())));
                                    Data zsdata = new Data();
                                    zsdata.setTM(bdms_st_astrotd_f.getYmdh());
                                    zsdata.setValue(zs);
                                    zsdataList.add(zsdata);
                                    zsdataList.sort(Comparator.comparing(Data::getTM));

                                    Data tdzdata = new Data();
                                    tdzdata.setTM(bdms_st_astrotd_f.getYmdh());
                                    tdzdata.setValue(tied.getTdz());
                                    tdzdataList.add(tdzdata);
                                    tdzdataList.sort(Comparator.comparing(Data::getTM));
                                }
                                if (bdms_st_astrotd_f.getHltdmk()==0){
                                    Data data = new Data();
                                    data.setTM(bdms_st_astrotd_f.getYmdh());
                                    data.setValue(bdms_st_astrotd_f.getFtdz());
                                    dataList.add(data);
                                    dataList.sort(Comparator.comparing(Data::getTM));
                                }else {
                                    String tiem =  bdms_st_astrotd_f.getYmdh().substring(0,10);
                                    ST_TIDE_R tide_r =  gdctide_rs.stream().filter(st_tide_r -> st_tide_r.getTm().substring(0,10).equals(tiem)&& st_tide_r.getHltdmk().equals(bdms_st_astrotd_f.getHltdmk())).findAny().orElse(null);
                                    if (tide_r!=null){
                                        HighAndLowTidesDatas tdzHighAndLowTidesDatas = new HighAndLowTidesDatas();
                                        HighAndLowTidesDatas zsHighAndLowTidesDatas = new HighAndLowTidesDatas();
                                        tdzHighAndLowTidesDatas.setValue(tide_r.getTdz());
                                        tdzHighAndLowTidesDatas.setTm(tide_r.getTm());
                                        tdzHighAndLowTidesDatas.setHltdmk(tide_r.getHltdmk());
                                        tdzgdc_lsit.add(tdzHighAndLowTidesDatas);
                                        zsHighAndLowTidesDatas.setValue(Double.parseDouble(String.format("%.2f", tide_r.getTdz()-bdms_st_astrotd_f.getFtdz())));
                                        zsHighAndLowTidesDatas.setTm(tide_r.getTm());
                                        zsHighAndLowTidesDatas.setHltdmk(tide_r.getHltdmk());
                                        zsgdc_lsit.add(zsHighAndLowTidesDatas);
                                    }
                                    HighAndLowTidesDatas twcHighAndLowTidesDatas = new HighAndLowTidesDatas();
                                    twcHighAndLowTidesDatas.setValue(bdms_st_astrotd_f.getFtdz());
                                    twcHighAndLowTidesDatas.setTm(bdms_st_astrotd_f.getYmdh());
                                    twcHighAndLowTidesDatas.setHltdmk(bdms_st_astrotd_f.getHltdmk());
                                    twcgdc_lsit.add(twcHighAndLowTidesDatas);
                                }
                            }
                            tdzTidalMonthlyData.setDatas(tdzdataList);
                            twcTidalMonthlyData.setDatas(dataList);
                            zsTidalMonthlyData.setDatas(zsdataList);
                            tdz_lsit.add(tdzTidalMonthlyData);
                            tdz_lsit.sort(Comparator.comparing(TidalMonthlyData::getHour));
                            twc_lsit.add(twcTidalMonthlyData);
                            twc_lsit.sort(Comparator.comparing(TidalMonthlyData::getHour));
                            zs_lsit.add(zsTidalMonthlyData);
                            zs_lsit.sort(Comparator.comparing(TidalMonthlyData::getHour));
                        }
                        anstauStatistics.setTides(tdz_lsit);
                        anstauStatistics.setTideHLs(tdzgdc_lsit);
                        anstauStatistics.setTwcHLs(twcgdc_lsit);
                        anstauStatistics.setTwcs(twc_lsit);
                        anstauStatistics.setZsHLs(zsgdc_lsit);
                        anstauStatistics.setZss(zs_lsit);
                        anstauStatisticsList.add(anstauStatistics);
                    }
                    anstauStatisticsList.sort(Comparator.comparing(AnstauStatistics::getDay));
                    anstauStatisticsResult.setDatas(anstauStatisticsList);
                    anstauStatisticsResults.add(anstauStatisticsResult);
                }
            }else {
                Map<String, List<ST_TIDE_R>> StcdListGroup = st_tide_rList.stream().collect(Collectors.groupingBy(ST_TIDE_R::getStcd, Collectors.toList()));
                for (Map.Entry<String, List<ST_TIDE_R>> stcdList : StcdListGroup.entrySet()) {
                    //单个返回对象
                    AnstauStatisticsResult anstauStatisticsResult = new AnstauStatisticsResult();
                    anstauStatisticsResult.setStationCode(stcdList.getKey());
                    String stnm =twcbdms_st_sensor_bs.stream().filter(bdms_st_sensor_b->bdms_st_sensor_b.getStcd().equals(stcdList.getKey())).findAny().get().getStnm();
                    anstauStatisticsResult.setStationName(stnm);
                    List<AnstauStatistics> anstauStatisticsList = new ArrayList<>();

                    //天文潮站码
                    String TWCSTCD =    twcbdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B->BDMS_ST_SENSOR_B.getDatacode().equals(stcdList.getKey())).findAny().get().getStcd();
                    //根据天文潮站码获取实测取数码
                    String TDZSTCD =  bdms_st_sensor_bs.stream().filter(BDMS_ST_SENSOR_B->BDMS_ST_SENSOR_B.getStcd().equals(TWCSTCD)).findAny().get().getDatacode();
                    //获取实测数据
                    List<ST_TIDE_R> st_tide_rs = st_tide_rList.stream().filter(ST_TIDE_R -> ST_TIDE_R.getStcd().equals(TDZSTCD)).collect(Collectors.toList());
                    //实测高低潮数据
                    List <ST_TIDE_R> gdctide_rs =  st_tide_rs.stream().filter(st_tide_r ->st_tide_r.getHltdmk()!=null &&st_tide_r.getHltdmk()>0).collect(Collectors.toList());
                    //数据根据天分组
                    Map<String, List<ST_TIDE_R>> DayListGroup = stcdList.getValue().stream().collect(Collectors.groupingBy(ST_TIDE_R::getDay, Collectors.toList()));
                    for (Map.Entry<String, List<ST_TIDE_R>> dayList : DayListGroup.entrySet()) {
                        AnstauStatistics anstauStatistics = new AnstauStatistics();
                        anstauStatistics.setDay(dayList.getKey());
                        //实测
                        List<TidalMonthlyData> tdz_lsit = new ArrayList<>();
                        List<HighAndLowTidesDatas> tdzgdc_lsit = new ArrayList<>();
                        //天文潮
                        List<TidalMonthlyData> twc_lsit = new ArrayList<>();
                        List<HighAndLowTidesDatas> twcgdc_lsit = new ArrayList<>();
                        //zs
                        List<TidalMonthlyData> zs_lsit = new ArrayList<>();
                        List<HighAndLowTidesDatas> zsgdc_lsit = new ArrayList<>();
                        //循环数据
                        Map<String, List<ST_TIDE_R>> HourListGroup = dayList.getValue().stream().collect(Collectors.groupingBy(ST_TIDE_R::getHour, Collectors.toList()));
                        for (Map.Entry<String, List<ST_TIDE_R>> hourList : HourListGroup.entrySet()) {
                            TidalMonthlyData tdzTidalMonthlyData = new TidalMonthlyData();
                            TidalMonthlyData twcTidalMonthlyData = new TidalMonthlyData();
                            TidalMonthlyData zsTidalMonthlyData = new TidalMonthlyData();

                            tdzTidalMonthlyData.setHour(hourList.getKey());
                            twcTidalMonthlyData.setHour(hourList.getKey());
                            zsTidalMonthlyData.setHour(hourList.getKey());
                            List<Data> dataList = new ArrayList<>();
                            List<Data> zsdataList = new ArrayList<>();
                            List<Data> tdzdataList = new ArrayList<>();
                            for (ST_TIDE_R bdms_st_astrotd_f:hourList.getValue()){
                                BDMS_ST_ASTROTD_F tied= bdmsStAstrotdFs.stream().filter(st_tide_r -> st_tide_r.getYmdh().substring(0,19).equals(bdms_st_astrotd_f.getTm().substring(0,19))).findAny().orElse(null);
                                if (tied!=null){
                                    double zs =(Double.parseDouble(String.format("%.2f", bdms_st_astrotd_f.getTdz()-tied.getFtdz())));
                                    Data zsdata = new Data();
                                    zsdata.setTM(bdms_st_astrotd_f.getTm());
                                    zsdata.setValue(zs);
                                    zsdataList.add(zsdata);
                                    zsdataList.sort(Comparator.comparing(Data::getTM));
                                    if (tied.getHltdmk()==0){
                                        Data data = new Data();
                                        data.setTM(tied.getYmdh());
                                        data.setValue(tied.getFtdz());
                                        dataList.add(data);
                                        dataList.sort(Comparator.comparing(Data::getTM));
                                    }else {
                                        String tiem =  tied.getYmdh().substring(0,10);
                                        ST_TIDE_R tide_r =  gdctide_rs.stream().filter(st_tide_r -> st_tide_r.getTm().substring(0,10).equals(tiem)&& st_tide_r.getHltdmk().equals(bdms_st_astrotd_f.getHltdmk())).findAny().orElse(null);
                                        if (tide_r!=null){
                                            HighAndLowTidesDatas zsHighAndLowTidesDatas = new HighAndLowTidesDatas();
                                            zsHighAndLowTidesDatas.setValue(Double.parseDouble(String.format("%.2f", bdms_st_astrotd_f.getTdz()-tied.getFtdz())));
                                            zsHighAndLowTidesDatas.setTm(tide_r.getTm());
                                            zsHighAndLowTidesDatas.setHltdmk(tide_r.getHltdmk());
                                            zsgdc_lsit.add(zsHighAndLowTidesDatas);
                                        }
                                        HighAndLowTidesDatas twcHighAndLowTidesDatas = new HighAndLowTidesDatas();
                                        twcHighAndLowTidesDatas.setValue(tied.getFtdz());
                                        twcHighAndLowTidesDatas.setTm(tied.getYmdh());
                                        twcHighAndLowTidesDatas.setHltdmk(tied.getHltdmk());
                                        twcgdc_lsit.add(twcHighAndLowTidesDatas);
                                    }

                                }
                                Data tdzdata = new Data();
                                tdzdata.setTM(bdms_st_astrotd_f.getTm());
                                tdzdata.setValue(bdms_st_astrotd_f.getTdz());
                                tdzdataList.add(tdzdata);
                                tdzdataList.sort(Comparator.comparing(Data::getTM));
                                if (bdms_st_astrotd_f.getHltdmk()!=null&&bdms_st_astrotd_f.getHltdmk()>0){
                                    HighAndLowTidesDatas tdzHighAndLowTidesDatas = new HighAndLowTidesDatas();

                                    tdzHighAndLowTidesDatas.setValue(bdms_st_astrotd_f.getTdz());
                                    tdzHighAndLowTidesDatas.setTm(bdms_st_astrotd_f.getTm());
                                    tdzHighAndLowTidesDatas.setHltdmk(bdms_st_astrotd_f.getHltdmk());
                                    tdzgdc_lsit.add(tdzHighAndLowTidesDatas);
                                }


                            }
                            tdzTidalMonthlyData.setDatas(tdzdataList);
                            twcTidalMonthlyData.setDatas(dataList);
                            zsTidalMonthlyData.setDatas(zsdataList);
                            tdz_lsit.add(tdzTidalMonthlyData);
                            tdz_lsit.sort(Comparator.comparing(TidalMonthlyData::getHour));
                            twc_lsit.add(twcTidalMonthlyData);
                            twc_lsit.sort(Comparator.comparing(TidalMonthlyData::getHour));
                            zs_lsit.add(zsTidalMonthlyData);
                            zs_lsit.sort(Comparator.comparing(TidalMonthlyData::getHour));
                        }
                        anstauStatistics.setTides(tdz_lsit);
                        anstauStatistics.setTideHLs(tdzgdc_lsit);
                        anstauStatistics.setTwcHLs(twcgdc_lsit);
                        anstauStatistics.setTwcs(twc_lsit);
                        anstauStatistics.setZsHLs(zsgdc_lsit);
                        anstauStatistics.setZss(zs_lsit);
                        anstauStatisticsList.add(anstauStatistics);
                    }
                    anstauStatisticsList.sort(Comparator.comparing(AnstauStatistics::getDay));
                    anstauStatisticsResult.setDatas(anstauStatisticsList);
                    anstauStatisticsResults.add(anstauStatisticsResult);
                }
            }
        }

        return  anstauStatisticsResults;
    }




    public <T> List<List<T>> splitDataIntoBatches(List<T> entities, int batchSize) {
        List<List<T>> batches = new ArrayList<>();
        for (int i = 0; i < entities.size(); i += batchSize) {
            int end = Math.min(entities.size(), i + batchSize);
            List<T> batch = entities.subList(i, end);
            batches.add(batch);

        }
        return batches;
    }
    //数据自动补齐
    public  int  autoDataPolishing(int projectId,String stationCode,String startTime, String endTime,Integer methodId,boolean saveData){
        List<RealTimeHydrologicData> realTimeHydrologicDatas = new ArrayList<>();
        List<RealTimeHydrologicData> realTimeHydrologicDataList = new ArrayList<>();
        List<Aruthmetic> aruthmetics = new ArrayList<>();
        List<BDMS_ST_TIDE_E> bdms_st_tide_es = new ArrayList<>();
        //根据项目id获取实测取数码
        List<String> stationCodes = new ArrayList<>();
        stationCodes.add(stationCode);
        String dataCode = bdms_st_sensor_bService.selectBySTCD29(projectId,stationCodes).get(0).getDatacode();
        List<String> dataCodes = new ArrayList<>();
        dataCodes.add(dataCode);
        //根据开始时间结束时间获取实测数据
        List<ST_TIDE_R> st_tide_rs = st_tide_rService.selectByStcd(dataCodes,startTime,endTime);
        //获取天文潮取数码
        String twcDataCode = bdms_st_sensor_bService.selectTwcByStcds(stationCodes).get(0).getDatacode();
        //根据开始时间结束时间获取天文潮数据
        TimeUtil timeUtil = new TimeUtil();
        String twcStartTime = timeUtil.dateAddYear(startTime,-1);

        String twcEndTime = timeUtil.dateAddYear(endTime,-1);

        List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs = bdms_st_astrotd_fService.selectTide(twcDataCode,twcStartTime,twcEndTime,0);
        //逐时
        //循环天文潮逐时数据
        List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFList=  bdmsStAstrotdFs.stream().filter(bdms_st_astrotd_f -> bdms_st_astrotd_f.getYmdh().startsWith("00", 14)).collect(Collectors.toList());
        System.out.println(bdmsStAstrotdFList.size());

        if (methodId==0){//线性
            for (BDMS_ST_ASTROTD_F bdms_st_astrotd_f : bdmsStAstrotdFList) {
                String tideTm = bdms_st_astrotd_f.getYmdh().substring(0, 19);
                ST_TIDE_R tide_r = st_tide_rs.stream().filter(st_tide_r -> st_tide_r.getTm().substring(0, 19).equals(tideTm)).findAny().orElse(null);
                RealTimeHydrologicData realTimeHydrologicData = new RealTimeHydrologicData();
                realTimeHydrologicData.setTime(tideTm);
                if (tide_r != null) {
                    realTimeHydrologicData.setTdz(tide_r.getTdz());
                }else {
                    realTimeHydrologicData.setTdz(null);
                }
                realTimeHydrologicDatas.add(realTimeHydrologicData);
            }
            for (int i=0;i<realTimeHydrologicDatas.size();i++){
                if (i+1<realTimeHydrologicDatas.size()){
                    String startTM=null;String endTM=null;
                    Double a =null; Double c=null;
                    if (realTimeHydrologicDatas.get(i).getTdz()==null){
                        if (realTimeHydrologicDatas.get(i-1).getTdz()!=null){
                            startTM =realTimeHydrologicDatas.get(i-1).getTime();
                            a =realTimeHydrologicDatas.get(i-1).getTdz();
                        }

                        if (realTimeHydrologicDatas.get(i+1).getTdz()!=null){
                            endTM =realTimeHydrologicDatas.get(i+1).getTime();
                            c =realTimeHydrologicDatas.get(i+1).getTdz();
                        }
                        if ((startTM!=null&&a!=null)||(endTM!=null&&c!=null)){
                            Aruthmetic aruthmetic = new Aruthmetic();
                            aruthmetic.setA(a);
                            aruthmetic.setC(c);
                            aruthmetic.setEndTm(endTM);
                            aruthmetic.setStartTm(startTM);
                            aruthmetics.add(aruthmetic);
                        }
                    }
                }
            }
            List<Aruthmetic> parmList = new ArrayList<>();
            List<Aruthmetic> nList = aruthmetics.stream().filter(aruthmetic -> (aruthmetic.getStartTm()==null&&aruthmetic.getA()==null)||
                    (aruthmetic.getEndTm()==null&&aruthmetic.getC()==null)).collect(Collectors.toList());
            for (int i=0;i< nList.size();i++){
                if(i % 2 == 0){
                    Aruthmetic aruthmetic = new Aruthmetic();
                    aruthmetic.setA(nList.get(i).getA());
                    aruthmetic.setStartTm(nList.get(i).getStartTm());
                    aruthmetic.setEndTm(nList.get(i+1).getEndTm());
                    aruthmetic.setC(nList.get(i+1).getC());
                    parmList.add(aruthmetic);

                }
            }
            parmList.addAll(aruthmetics.stream().filter(aruthmetic -> !(aruthmetic.getStartTm()==null&&aruthmetic.getA()==null)&&
                    !(aruthmetic.getEndTm()==null&&aruthmetic.getC()==null)).collect(Collectors.toList()));
            for (Aruthmetic aruthmetic:parmList){
                List<RealTimeHydrologicData> RealTimeHydrologicData = arithmeticUtil.getInterpolationTDZ(aruthmetic.getStartTm()
                        ,aruthmetic.getEndTm(),aruthmetic.getA(),aruthmetic.getC(),60);
                realTimeHydrologicDataList.addAll(RealTimeHydrologicData);
            }


            for (RealTimeHydrologicData realTimeHydrologicData:realTimeHydrologicDataList){
                BDMS_ST_TIDE_E bdms_st_tide_e = new BDMS_ST_TIDE_E();
                bdms_st_tide_e.setPid(projectId);
                bdms_st_tide_e.setStcd(stationCode);
                bdms_st_tide_e.setTm(realTimeHydrologicData.getTime());
                bdms_st_tide_e.setTdz(realTimeHydrologicData.getTdz());
                bdms_st_tide_e.setHltdmk(0);
                bdms_st_tide_es.add(bdms_st_tide_e);
            }



        }else if (methodId==1){//接入天文潮
            for (BDMS_ST_ASTROTD_F bdms_st_astrotd_f : bdmsStAstrotdFList) {
                String tideTm= bdms_st_astrotd_f.getYmdh().substring(0,19);
                tideTm =timeUtil.dateAddYear(tideTm,+1);
                String finalTideTm = tideTm;
                ST_TIDE_R tide_r= st_tide_rs.stream().filter(st_tide_r -> st_tide_r.getTm().substring(0,19).equals(finalTideTm)).findAny().orElse(null);
                if (tide_r == null) {
                    BDMS_ST_TIDE_E bdms_st_tide_e = new BDMS_ST_TIDE_E();
                    bdms_st_tide_e.setPid(projectId);
                    bdms_st_tide_e.setTm(tideTm);
                    bdms_st_tide_e.setStcd(stationCode);
                    bdms_st_tide_e.setTdz(bdms_st_astrotd_f.getFtdz());
                    bdms_st_tide_e.setHltdmk(bdms_st_astrotd_f.getHltdmk());
                    //    System.out.println(bdms_st_tide_e);
                    bdms_st_tide_es.add(bdms_st_tide_e);
                }

            }
        }
        //先删后入
        bdms_st_tide_eMapper.deleteByPId(projectId,stationCode,startTime,endTime);

        List<List<BDMS_ST_TIDE_E>> batches = splitDataIntoBatches(bdms_st_tide_es, 100);
        int res =0;
        if (saveData){
            //res = bdms_st_tide_eService.insertBatch(bdms_st_tide_es);
            for (List<BDMS_ST_TIDE_E> batch : batches) {
                // 插入当前批次的数据
                int res_b=bdms_st_tide_eService.insertBatch(batch);
                res += res_b;
            }
        }
        return res;
    }


    //数据保存
    public String dataPreservation(JSONObject jsonObject){
        List<BDMS_ST_TIDE_E> bdms_st_tide_es = new ArrayList<>();

        Integer projectId  =  jsonObject.getInteger("projectId");
        String stationCode =  jsonObject.getString("stationCode");


        JSONArray jsonArray = jsonObject.getJSONArray("data");

        for (Object o : jsonArray) {
            LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>) o;
            BDMS_ST_TIDE_E data = new BDMS_ST_TIDE_E();
            data.setTm((String) linkedHashMap.get("tm"));
            data.setTdz(Double.valueOf(linkedHashMap.get("value").toString()));
            data.setPid(projectId);
            data.setStcd(stationCode);
            bdms_st_tide_es.add(data);
        }
        int res = bdms_st_tide_eService.insertBatch(bdms_st_tide_es);
        return  "已成功保存，保存条数为： "+res;
    }


    //获取调和分析常数
    public  List<StrmsrgHarInfo> getBlendingConstant(Integer projectId, String stationCode){
        String tag =projectId+"x";
        List<StrmsrgHarInfo> infos = new ArrayList<>();

        List<bdms_forecastparameters_b> bdmsForecastparametersBs =   bdms_forecastparameters_bMapper.getBaseDatByFschemeId(stationCode,tag);

        for (bdms_forecastparameters_b bdmsForecastparametersB : bdmsForecastparametersBs) {
            if (bdmsForecastparametersB !=null){


                StrmsrgHarInfo strmsrgHarInfo = new StrmsrgHarInfo();
                strmsrgHarInfo.setHarscheme(bdmsForecastparametersB.getPschemenm());
                strmsrgHarInfo.setHarschemeAbb(bdmsForecastparametersB.getPschemenm());
                strmsrgHarInfo.setHarmonicdata(bdmsForecastparametersB.getParameters());
                strmsrgHarInfo.setStcd(stationCode);
                strmsrgHarInfo.setTime(bdmsForecastparametersB.getBasedatas());
                // strmsrgHarInfo.setHarmonicfilenm(strmsrg_harinfo.getHarmonicfilenm());
                strmsrgHarInfo.setParamSchemeId(bdmsForecastparametersB.getPschemeid());
                strmsrgHarInfo.setCreationTime(bdmsForecastparametersB.getCreatetime());
                infos.add(strmsrgHarInfo);
            }
        }

        return     infos;
    }
    //获取单个调和分析常数
    //天文潮预报计算
    //天文潮预报计算

    public TideForecastResult TideForecast(String startTime, String endTime, List<String> paramSchemeIds ) throws Exception{


        List<BDMS_ST_ASTROTD_F> finalDatas = new ArrayList<>();
        StringBuilder builder = new StringBuilder();
        //创建文件夹
        String type = "TideForecast";
        getInformTxtUtil.mkdirFile(type);

        //根据参数方案编号集合获取参数方案信息
        List<bdms_forecastparameters_b> bdmsForecastparametersBs =   bdms_forecastparameters_bMapper.queryByParamIds(paramSchemeIds);
        //不为空并且大于零
        Jedis jedis = new Jedis(redisUri);
        //存入预报时间信息
        jedis.set("startTime",startTime);
        jedis.set("endTime",endTime);
        if(bdmsForecastparametersBs!=null&&bdmsForecastparametersBs.size()>0){
            TideForecastResult tideForecastResult = new TideForecastResult();
            List<ParamData> avgDatas = new ArrayList<>();
            List<ParamResult> paramResults = new ArrayList<>();
            List<List<BDMS_ST_ASTROTD_F>> hourDatas = new ArrayList<>();
            List<List<BDMS_ST_ASTROTD_F>> gdcDatas = new ArrayList<>();
            //循环参数方案获取不同调和常数预报
            for (bdms_forecastparameters_b bdmsForecastparametersB : bdmsForecastparametersBs) {
                ParamResult paramResult = new ParamResult();
                List<ParamData> paramDatas = new ArrayList<>();

                paramResult.setParamSchemeId(bdmsForecastparametersB.getPschemeid());
                paramResult.setParamSchemeName(bdmsForecastparametersB.getPschemenm());
                String scheme =type+ File.separator + getInformTxtUtil.getUUID();
                //创建方案编号文件
                getInformTxtUtil.mkdirFile(scheme);
                builder.append(scheme).append("\n");
                //创建inform，txt
                String time  =endTime.substring(2,4);
                getInformTxtUtil.getTideForecastInformTxt("TideForecast",scheme,startTime,endTime,serverType,time);
                //根据站点调和分析常数数据创建文件

                String[] array = bdmsForecastparametersB.getParameters().split("\\\\n");
                StringBuilder sb = new StringBuilder();
                for (String str : array){
                    sb.append(str).append("\n");
                }

                //System.out.println(sb.toString());
                getInformTxtUtil.tiedYearRes(time,scheme,sb.toString());//执行命令
                if ("liunx".equals(serverType)){
                    String  zx = getInformTxtUtil.executeLiunx28("TideForecast", scheme);
                    builder.append(zx).append("\n");
                }else {
                    getInformTxtUtil.execute("TideForecast", scheme);
                }
                //获取到lvl文件后保存值redis
                List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs = GetInformTxtUtil.readToList(GetInformTxtUtil.getCourseFile()+File.separator+"temp"+File.separator+scheme+File.separator+"SHTIDEcw.lvl","GBK");
                bdmsStAstrotdFs.sort(Comparator.comparing(BDMS_ST_ASTROTD_F::getYmdh));
                //
                if (bdmsStAstrotdFs.size()>0){
                    String json = JSON.toJSONString(bdmsStAstrotdFs);
//                    if (jedis.exists(bdmsForecastparametersB.getPschemeid())) {
//                        jedis.del(bdmsForecastparametersB.getPschemeid());
//                    }
                    // 将 JSON 字符串存储到 Redis 中
                    jedis.set(bdmsForecastparametersB.getPschemeid(), json);
                    hourDatas.add(bdmsStAstrotdFs.stream().filter(bdmsStAstrotdF -> bdmsStAstrotdF.getHltdmk() != null && bdmsStAstrotdF.getHltdmk()==0).collect(Collectors.toList()));
                    gdcDatas.add(bdmsStAstrotdFs.stream().filter(bdmsStAstrotdF -> bdmsStAstrotdF.getHltdmk() != null && bdmsStAstrotdF.getHltdmk()>0).collect(Collectors.toList()));
                    //bdmsStAstrotdFs按照月份分组
                    Map<String, List<BDMS_ST_ASTROTD_F>> MonthListGroup = bdmsStAstrotdFs.stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getDateTime, Collectors.toList()));
                    for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> MonthList : MonthListGroup.entrySet()) {
                        ParamData paramData = new ParamData();
                        paramData.setMonth(MonthList.getKey());
                        List<TidalMonthlyData> tidalMonthlyDataList = new ArrayList<>();
                        List<HighAndLowTidesDatas> highAndLowTidesDatas =new ArrayList<>();

                        Map<String, List<BDMS_ST_ASTROTD_F>> DayListGroup = MonthList.getValue().stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getDay, Collectors.toList()));
                        for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> DayList : DayListGroup.entrySet()) {
                            TidalMonthlyData tidalMonthlyData = new TidalMonthlyData();
                            tidalMonthlyData.setDate(DayList.getKey());
                            //返回对象
                            List<Data> datas = new ArrayList<>();
                            for (BDMS_ST_ASTROTD_F bdms_st_astrotd_f : DayList.getValue()) {
                                Data data = new Data();
                                data.setTM(bdms_st_astrotd_f.getYmdh().substring(0, 19));
                                data.setValue(bdms_st_astrotd_f.getFtdz());
                                data.setHltdmk(bdms_st_astrotd_f.getHltdmk());
                                if (bdms_st_astrotd_f.getHltdmk() != null && bdms_st_astrotd_f.getHltdmk()==0){
                                    datas.add(data);
                                }else {
                                    HighAndLowTidesDatas highAndLowTidesDatas1 = new HighAndLowTidesDatas();
                                    highAndLowTidesDatas1.setTm(bdms_st_astrotd_f.getYmdh());
                                    highAndLowTidesDatas1.setValue(bdms_st_astrotd_f.getFtdz());
                                    highAndLowTidesDatas1.setHltdmk(bdms_st_astrotd_f.getHltdmk());
                                    highAndLowTidesDatas.add(highAndLowTidesDatas1);
                                }
                            }
                            datas.sort(Comparator.comparing(Data::getTM));
                            tidalMonthlyData.setDatas(datas);
                            tidalMonthlyDataList.add(tidalMonthlyData);
                            tidalMonthlyDataList.sort(Comparator.comparing(TidalMonthlyData::getDate));
                        }
                        highAndLowTidesDatas.sort(Comparator.comparing(HighAndLowTidesDatas::getTm));
                        paramData.setTideHLDatas(highAndLowTidesDatas);
                        paramData.setTidalMonthlyDatas(tidalMonthlyDataList);
                        paramDatas.add(paramData);
                    }
                }
                paramDatas.sort(Comparator.comparing(ParamData::getMonth));
                paramResult.setParamDatas(paramDatas);
                paramResults.add(paramResult);
            }
            //如果只有一条方案直接返回结构
            if (bdmsForecastparametersBs.size()==1){
                avgDatas = paramResults.get(0).getParamDatas();
            }else {
                TimeUtil timeUtil = new TimeUtil();
                //求平均值
                List<BDMS_ST_ASTROTD_F> allData = hourDatas.stream()
                        .flatMap(List::stream)
                        .collect(Collectors.toList());


                String stcd = allData.get(0).getStcd();
                // 按 ymdh 进行分组，并计算 ftdz 的平均值
                Map<String, Double> averageFtdzByYmdh = allData.stream()
                        .collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getYmdh,
                                Collectors.averagingDouble(BDMS_ST_ASTROTD_F::getFtdz)));
                for (Map.Entry<String, Double> stringDoubleEntry : averageFtdzByYmdh.entrySet()) {
                    String ymdh = stringDoubleEntry.getKey();
                    Double ftdz = stringDoubleEntry.getValue();
                    //创建新的入库对象
                    BDMS_ST_ASTROTD_F bdms_st_astrotd_f = new BDMS_ST_ASTROTD_F();
                    bdms_st_astrotd_f.setStcd(stcd);
                    bdms_st_astrotd_f.setYmdh(ymdh);
                    bdms_st_astrotd_f.setFtdz(ftdz);
                    bdms_st_astrotd_f.setHltdmk(0);
                    bdms_st_astrotd_f.setUnitname(4);
                    finalDatas.add(bdms_st_astrotd_f);
                }
                //高低潮平均值

                List<BDMS_ST_ASTROTD_F> gdcData = gdcDatas.stream()
                        .flatMap(List::stream)
                        .collect(Collectors.toList());
                // 按 ymdh 进行分组，并计算 ftdz 的平均值
                Map<String, List<BDMS_ST_ASTROTD_F>> averageFtdzByDay = gdcData.stream()
                        .collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getDay));
                for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> stringDoubleEntry : averageFtdzByDay.entrySet()) {
                    //按照hltdmk分组
                    Map<Integer, List<BDMS_ST_ASTROTD_F>> averageFtdzByHltdmk = stringDoubleEntry.getValue().stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getHltdmk));
                    for (Map.Entry<Integer, List<BDMS_ST_ASTROTD_F>> integerListEntry : averageFtdzByHltdmk.entrySet()) {
                        BDMS_ST_ASTROTD_F gdc = new BDMS_ST_ASTROTD_F();
                        List<String> times = new ArrayList<>();
                        gdc.setHltdmk(integerListEntry.getKey());
                        gdc.setFtdz(integerListEntry.getValue().stream().mapToDouble(BDMS_ST_ASTROTD_F::getFtdz).average().orElse(0.0));
                        for (BDMS_ST_ASTROTD_F bdms_st_astrotd_f : integerListEntry.getValue()) {
                            times.add(bdms_st_astrotd_f.getYmdh());
                        }
                        String avgTime =  timeUtil.averageTime(times);
                        gdc.setYmdh(avgTime);
                        gdc.setUnitname(4);
                        finalDatas.add(gdc);
                    }
                }

                String json = JSON.toJSONString(finalDatas);
                jedis.set("avgDatas",json);
                Map<String, List<BDMS_ST_ASTROTD_F>> MonthListGroup = finalDatas.stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getDateTime, Collectors.toList()));
                for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> MonthList : MonthListGroup.entrySet()) {
                    ParamData paramData = new ParamData();
                    paramData.setMonth(MonthList.getKey());
                    List<TidalMonthlyData> tidalMonthlyDataList = new ArrayList<>();
                    List<HighAndLowTidesDatas> highAndLowTidesDatas =new ArrayList<>();
                    Map<String, List<BDMS_ST_ASTROTD_F>> DayListGroup = MonthList.getValue().stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getDay, Collectors.toList()));
                    for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> DayList : DayListGroup.entrySet()) {
                        TidalMonthlyData tidalMonthlyData = new TidalMonthlyData();
                        tidalMonthlyData.setDate(DayList.getKey());
                        //返回对象
                        List<Data> datas = new ArrayList<>();
                        for (BDMS_ST_ASTROTD_F bdms_st_astrotd_f : DayList.getValue()) {
                            Data data = new Data();
                            data.setTM(bdms_st_astrotd_f.getYmdh().substring(0, 19));
                            data.setValue(bdms_st_astrotd_f.getFtdz());
                            data.setHltdmk(bdms_st_astrotd_f.getHltdmk());
                            if (bdms_st_astrotd_f.getHltdmk() != null && bdms_st_astrotd_f.getHltdmk() == 0) {
                                datas.add(data);
                            }else {
                                HighAndLowTidesDatas highAndLowTidesDatas1 = new HighAndLowTidesDatas();
                                highAndLowTidesDatas1.setTm(bdms_st_astrotd_f.getYmdh());
                                highAndLowTidesDatas1.setValue(bdms_st_astrotd_f.getFtdz());
                                highAndLowTidesDatas1.setHltdmk(bdms_st_astrotd_f.getHltdmk());
                                highAndLowTidesDatas.add(highAndLowTidesDatas1);
                            }
                        }
                        datas.sort(Comparator.comparing(Data::getTM));
                        tidalMonthlyData.setDatas(datas);
                        tidalMonthlyDataList.add(tidalMonthlyData);
                        tidalMonthlyDataList.sort(Comparator.comparing(TidalMonthlyData::getDate));
                    }
                    highAndLowTidesDatas.sort(Comparator.comparing(HighAndLowTidesDatas::getTm));
                    paramData.setTideHLDatas(highAndLowTidesDatas);
                    paramData.setTidalMonthlyDatas(tidalMonthlyDataList);
                    avgDatas.add(paramData);

                }
            }
            tideForecastResult.setParamResults(paramResults);
            avgDatas.sort(Comparator.comparing(ParamData::getMonth));
            tideForecastResult.setAvgDatas(avgDatas);
            return  tideForecastResult;
        }else {
            return null;
        }




    }
    //根据参数方案集合创建保存的数据入库
    public String  saveTideData(Integer projectId, String paramSchemeId,String userId,String stationCode) throws Exception {
        Jedis jedis = new Jedis(redisUri);
        String startTime = jedis.get("startTime");
        String endTime = jedis.get("endTime");
        List<BDMS_ST_ASTROTD_F> finalDatas = new ArrayList<>();
        String stcd  =stationCode;
         String X = projectId+"x";
        if (jedis.exists(paramSchemeId)){
            String jsonString = jedis.get(paramSchemeId);
            finalDatas = JSON.parseArray(jsonString, BDMS_ST_ASTROTD_F.class);
            finalDatas.forEach(bdms_st_astrotd_f -> bdms_st_astrotd_f.setStcd(stcd));

            if (stcd!=null && !"".equals(stcd)) {
                TimeUtil timeUtil = new TimeUtil();
                BDMS_CALCULATESCHEME_M1 bdms_calculatescheme_m = new BDMS_CALCULATESCHEME_M1();
                bdms_calculatescheme_m.setCschemeid(getInformTxtUtil.getUUID());
                bdms_calculatescheme_m.setPid(projectId);
                bdms_calculatescheme_m.setCschemenm("潮汐预报");
                bdms_calculatescheme_m.setFschemeid(X);
                bdms_calculatescheme_m.setDtmstart(startTime);
                bdms_calculatescheme_m.setDtmforecast(startTime);
                bdms_calculatescheme_m.setDtmend(endTime);
                bdms_calculatescheme_m.setUserid(userId);
                bdms_calculatescheme_m.setParschemeid(startTime.substring(0, 4));
                bdms_calculatescheme_m.setTag(stcd);
                bdms_calculatescheme_m.setDtmcalculate(timeUtil.currentTime());
                bdms_calculatescheme_m1Mapper.deleteByFschemeId(X, stcd);

                //创建预报方案
                bdms_calculatescheme_m1Mapper.insertBdmsCalculateschemeM(bdms_calculatescheme_m);

                //天文潮数据删除
                bdms_st_astrotd_fMapper.deleteByStationCode(startTime, endTime, stcd);

                //计算完成后 数据入库
                int res = getInformTxtUtil.insert(finalDatas);
                if (res > 0) {
                    return "保存成功";
                } else {
                    return "保存失败";
                }
            }else {
                return "已保存此站点其他方案结果";
            }

        }else {
            return "此方案暂未预报计算，无法保存";
        }


    }


    //获取实测 和 _E 表数据 GetHarmonicAnalysisTide
    public IntegrationResult GetHarmonicAnalysisTide(int projectId,String stationCode,String startTime, String endTime) throws Exception{
        IntegrationResult integrationResult = new IntegrationResult();
        List<Integration> Integrations = new ArrayList<>();
        integrationResult.setStationCode(stationCode);

        //根据
        //根据项目id获取实测取数码
        List<String> stationCodes = new ArrayList<>();
        stationCodes.add(stationCode);
        String dataCode = bdms_st_sensor_bService.selectBySTCD29(projectId,stationCodes).get(0).getDatacode();
        String stationName = bdms_st_sensor_bService.selectBySTCD29(projectId,stationCodes).get(0).getStnm();
        integrationResult.setStationName(stationName);
        List<String> dataCodes = new ArrayList<>();
        dataCodes.add(dataCode);
        //根据开始时间结束时间获取实测数据
        List<ST_TIDE_R> st_tide_rs = st_tide_rService.selectByStationArrys(dataCodes,startTime,endTime);
        if (st_tide_rs!=null && st_tide_rs.size()>0){
            st_tide_rs.sort(Comparator.comparing(ST_TIDE_R::getTm));
            integrationResult.setDataStartTime(st_tide_rs.get(0).getTm());
            integrationResult.setDataEndTime(st_tide_rs.get(st_tide_rs.size()-1).getTm());
        }
        //获取天文潮取数码
        String twcDataCode = bdms_st_sensor_bService.selectTwcByStcds(stationCodes).get(0).getDatacode();
        //根据开始时间结束时间获取天文潮数据
        List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFs = bdms_st_astrotd_fService.selectTide(twcDataCode,startTime,endTime,0);


        //根据开始时间结束时间站码项目id 获取_e数据
        List<BDMS_ST_TIDE_E> bdms_st_tide_eList =  bdms_st_tide_eService.queryAllByPid(projectId,stationCode,startTime,endTime);
        //逐时
        //循环天文潮逐时数据
        List<BDMS_ST_ASTROTD_F> bdmsStAstrotdFList=  bdmsStAstrotdFs.stream().filter(bdms_st_astrotd_f -> bdms_st_astrotd_f.getYmdh().startsWith("00", 14)).collect(Collectors.toList());
        //根据月份分组
        Map<String, List<BDMS_ST_ASTROTD_F>> MonthListGroup = bdmsStAstrotdFList.stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getDateTime, Collectors.toList()));
        for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> MonthList : MonthListGroup.entrySet()) {
            Integration integration = new Integration();
            List<TidalMonthlyData> tidalMonthlyDataList = new ArrayList<>();

            System.out.println(MonthList.getKey());
            integration.setMonth(MonthList.getKey().substring(5, 7));
            Map<String, List<BDMS_ST_ASTROTD_F>> DayListGroup = MonthList.getValue().stream().collect(Collectors.groupingBy(BDMS_ST_ASTROTD_F::getDay, Collectors.toList()));
            for (Map.Entry<String, List<BDMS_ST_ASTROTD_F>> DayList : DayListGroup.entrySet()) {
                TidalMonthlyData tidalMonthlyData = new TidalMonthlyData();

                tidalMonthlyData.setDate(DayList.getKey());

                //返回对象
                List<Data> datas = new ArrayList<>();

                for (BDMS_ST_ASTROTD_F bdms_st_astrotd_f : DayList.getValue()) {
                    Data data = new Data();

                    String tideTm = bdms_st_astrotd_f.getYmdh().substring(0, 19);
                    ST_TIDE_R tide_r = st_tide_rs.stream().filter(st_tide_r -> st_tide_r.getTm().substring(0, 19).equals(tideTm)).findAny().orElse(null);
                    if (tide_r == null) {
                        BDMS_ST_TIDE_E tide_e = bdms_st_tide_eList.stream().filter(st_tide_e -> st_tide_e.getTm().substring(0, 19).equals(tideTm)).findAny().orElse(null);
                        if (tide_e != null) {
                            data.setTM(tideTm);
                            data.setValue(tide_e.getTdz());
                            datas.add(data);
                        }
                    } else {
                        data.setTM(tideTm);
                        data.setValue(tide_r.getTdz());
                        datas.add(data);
                    }

                }
                tidalMonthlyData.setDatas(datas);

                tidalMonthlyDataList.add(tidalMonthlyData);
                tidalMonthlyDataList.sort(Comparator.comparing(TidalMonthlyData::getDate));

            }
            integration.setTideDatas(tidalMonthlyDataList);

            Integrations.add(integration);
        }
        Integrations.sort(Comparator.comparing(Integration::getMonth));
        integrationResult.setIntegrations(Integrations);
        return  integrationResult;
    }
    public List<Data> sT_TIDE_r(List<ST_TIDE_R> st_tide_rs) {
        return st_tide_rs.stream()
                .map(stTideR -> {
                    Data data = new Data();
                    data.setTM(stTideR.getTm().substring(0, 19));
                    data.setValue(Double.valueOf(df.format(stTideR.getTdz())));
                    return data;
                })
                .collect(Collectors.toList());
    }
    public List<Data> sT_TIDE_e(List<BDMS_ST_TIDE_E> st_tide_rs) {
        return st_tide_rs.stream()
                .map(stTideR -> {
                    Data data = new Data();
                    data.setTM(stTideR.getTm().substring(0, 19));
                    data.setValue(Double.valueOf(df.format(stTideR.getTdz())));
                    return data;
                })
                .collect(Collectors.toList());
    }
    public JsonResult GetHarmonicAnalysis(int projectId, String stationCode, String stationName, String startTime, String endTime, String creationTime) throws Exception {
        String baseDatas = startTime.substring(0,16) +"-"+ endTime.substring(0,16);

        JsonResult jsonResult = new JsonResult();
        String mainId =projectId+"x";
        //查询是否存在相同调和分析参数
        bdms_forecastparameters_b forecastparameters_b=   bdms_forecastparameters_bMapper.queryByTag(baseDatas,stationCode,mainId);
        //判断forecastparameters_b是否为空

        if(forecastparameters_b==null) {
            String res_read =null;
            calendar.setTime(format.parse(startTime));
            calendar.add(Calendar.HOUR, -1);
            Date startDate = calendar.getTime();
            calendar.setTime(format.parse(endTime));
            calendar.add(Calendar.HOUR, +1);
            Date endDate  = calendar.getTime();


            //查应用时间范围内的实测数据
            List<String> stationCodes = new ArrayList<>();

            String dataCode = bdms_st_sensor_bService.selectOneByStcd(projectId,stationCode).getDatacode();
            stationCodes.add(dataCode);
            List<ST_TIDE_R> st_tide_rs = st_tide_rService.selectByStcd(stationCodes,startTime,endTime);

            //根据开始时间结束时间站码项目id 获取_e数据
            List<BDMS_ST_TIDE_E> bdms_st_tide_eList =  bdms_st_tide_eService.queryAllByPid(projectId,stationCode,startTime,endTime);

            List<Data> datas = new ArrayList<>();
            datas.addAll(sT_TIDE_e(bdms_st_tide_eList));
            datas.addAll(sT_TIDE_r(st_tide_rs));

            // 去除重复的Data对象
            Set<Data> uniqueDatas = new HashSet<>(datas);
            System.out.println("uniqueDatas: " + uniqueDatas.size());
            // 清空原始的datas列表
            datas.clear();

            // 将去重后的Data对象重新添加到datas列表中
            datas.addAll(uniqueDatas);

            //获取实测数据（不含高低拆）
            //List<Data> realTimeHydrologicDataList = datas.stream().filter(Data -> Data.getTM().startsWith("00", 14)).collect(Collectors.toList());
            //根据时间获取数据运用时间

            List<Data> yydatas = datas.stream().filter(Data -> {
                        try {
                            return format.parse(Data.getTM()).after(startDate) && format.parse(Data.getTM()).before(endDate);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        return false;
                    }
            ).sorted(Comparator.comparing(Data::getTM)).collect(Collectors.toList());
            //根据开始时间结束时间判断数据完整性
            long lDate1 = startDate.getTime();
            Date date  =endDate;
            calendar.add(Calendar.HOUR, -1);
            date  = calendar.getTime();
            long lDate2 = date.getTime();
            long diff =lDate2-lDate1;
            long hour = diff / (60 * 60 * 1000)  ;
            System.out.println(hour);
            //不缺少数据 进行分析
            //获取实测数据
            List<Integer> tdzs = new ArrayList<>();

            for (Data yyrealTimeHydrologicData : yydatas) {
                if (yyrealTimeHydrologicData.getValue() != null) {
                    tdzs.add((int) (yyrealTimeHydrologicData.getValue() * 100));
                }
            }

            if (tdzs.size()==hour){
                String type = "HarmonicAnalysis";
                getInformTxtUtil.mkdirFile(type);
                String scheme =type+ File.separator + getInformTxtUtil.getUUID();
                //创建方案编号文件
                getInformTxtUtil.mkdirFile(scheme);
                //生成inform.txt文件
                getInformTxtUtil.getTHFXInformTxt( "TideForecast",scheme,serverType);
                //写入实测数据
                getInformTxtUtil.getOBSEPHAR(stationCode,stationName,startTime,endTime,tdzs,"TideForecast",scheme);
                //执行计算
                String res ;
                String out ;
                if ("liunx".equals(serverType)) {

                    getInformTxtUtil.executeLiunx28("TideForecast",scheme);
                    res =startTime.substring(2,4)+"HG.RES";
                    out =startTime.substring(2,4)+"ER.OUT";
                }else {

                    getInformTxtUtil.execute("TideForecast",scheme);
                    res ="TIDE"+startTime.substring(2,4)+"HG.RES";
                    out ="TIDE"+startTime.substring(2,4)+"ER.OUT";
                }

                //计算完成数据入库
                //读取数据
                String courseFile=  GetInformTxtUtil.getCourseFile()+File.separator;

                res_read = getInformTxtUtil.read(courseFile+"temp"+File.separator +scheme+File.separator +res,"GBK");
                TimeUtil timeUtil = new TimeUtil();
                bdms_forecastsubscheme_b  bdmsForecastsubschemeB = new bdms_forecastsubscheme_b();
                String subSchemeId = getInformTxtUtil.getUUID20();

                bdmsForecastsubschemeB.setMainfschemeid(mainId);
                bdmsForecastsubschemeB.setSubfschemenm(stationName+"_调和分析");
                bdmsForecastsubschemeB.setFstcd(stationCode);
                bdmsForecastsubschemeB.setFstnm(stationName);
                bdmsForecastsubschemeB.setSubfschemeid(subSchemeId);
                bdmsForecastsubschemeB.setPid(projectId);
                //先删后入

                bdms_forecastsubscheme_bMapper.deleteByStcd(mainId,stationCode);
                //插入
                bdms_forecastsubscheme_bMapper.insert(bdmsForecastsubschemeB);
                //解析res文件后入库
                bdms_forecastparameters_b bdmsForecastparametersB = new bdms_forecastparameters_b();
                bdmsForecastparametersB.setPid(projectId);
                String crrTime = timeUtil.currentTime();
                //String subSchemeId = getInformTxtUtil.getUUID().substring(0,20);
                String parSchemeId = getInformTxtUtil.getUUID().substring(0,10);
                bdmsForecastparametersB.setPschemeid(parSchemeId);
                bdmsForecastparametersB.setPschemenm(stationName+"_"+startTime.substring(0,4)+"_"+crrTime.substring(0,16));
                bdmsForecastparametersB.setFschemeid(subSchemeId);
                bdmsForecastparametersB.setBasedatas(startTime.substring(0,16)+"-"+endTime.substring(0,16));
                bdmsForecastparametersB.setParameters(res_read);
                bdmsForecastparametersB.setCreatetime(crrTime);
                bdmsForecastparametersB.setUpdatetime(crrTime);
                bdmsForecastparametersB.setFlag("1");
                bdmsForecastparametersB.setRemark(stationCode);
                bdmsForecastparametersB.setTag(mainId);
                //先删后入
                // bdms_forecastparameters_bMapper.deleteByParamId(stationCode,bdmsForecastparametersB.getPschemeid(),mainId);
                //genju
                bdms_forecastparameters_bMapper.insert(bdmsForecastparametersB);
                // 解析OUT文件
                List<OutFileData> dataList =  getInformTxtUtil.GetOUTFileData(courseFile+"temp/"+scheme+"/"+out);
                // List<OutFileData> dataList =  getInformTxtUtil.GetOUTFileData("e:\\\\23ER.OUT");

                //解析data为IntegrationResult格式
                IntegrationResult integrationResult = new IntegrationResult();
                List<Integration> integrations = new ArrayList<>();
                integrationResult.setStationCode(stationCode);
                integrationResult.setStationName(stationName);
                //入库result_p
                //根据月分组 再根据日分组
                Map<String,List<OutFileData>> MonthListGroup = dataList.stream().collect(Collectors.groupingBy(OutFileData::getMonth));
                for (Map.Entry<String, List<OutFileData>> MonthList : MonthListGroup.entrySet()) {
                    Integration integration = new Integration();
                    List<TidalMonthlyData> tidalMonthlyDataList = new ArrayList<>();
                    List<TidalMonthlyData> ftdzMonthlyDataList = new ArrayList<>();
                    List<TidalMonthlyData> zsMonthlyDataList = new ArrayList<>();
                    integration.setMonth(MonthList.getKey().substring(5, 7));
                    Map<String, List<OutFileData>> DayListGroup = MonthList.getValue().stream().collect(Collectors.groupingBy(OutFileData::getDay, Collectors.toList()));
                    for (Map.Entry<String, List<OutFileData>> DayList : DayListGroup.entrySet()) {
                        TidalMonthlyData tidalMonthlyData = new TidalMonthlyData();
                        TidalMonthlyData ftdzMonthlyData = new TidalMonthlyData();
                        TidalMonthlyData zsMonthlyData = new TidalMonthlyData();
                        tidalMonthlyData.setDate(DayList.getKey());
                        ftdzMonthlyData.setDate(DayList.getKey());
                        zsMonthlyData.setDate(DayList.getKey());
                        //返回对象
                        List<Data> tdzdatas = new ArrayList<>();
                        List<Data> twcdatas = new ArrayList<>();
                        List<Data> zsdatas = new ArrayList<>();

                        //获取每个日期的数据
                        for (OutFileData outFileData : DayList.getValue()) {
                            String tm = outFileData.getTm();
                            tdzdatas.add(new Data(tm,outFileData.getTdz()));
                            twcdatas.add(new Data(tm,outFileData.getTwc()));
                            zsdatas.add(new Data(tm,outFileData.getZs()));
                        }
                        tdzdatas.sort(Comparator.comparing(Data::getTM));
                        tidalMonthlyData.setDatas(tdzdatas);
                        twcdatas.sort(Comparator.comparing(Data::getTM));
                        ftdzMonthlyData.setDatas(twcdatas);
                        zsdatas.sort(Comparator.comparing(Data::getTM));
                        zsMonthlyData.setDatas(zsdatas);
                        zsMonthlyDataList.add(zsMonthlyData);
                        ftdzMonthlyDataList.add(ftdzMonthlyData);
                        tidalMonthlyDataList.add(tidalMonthlyData);
                    }
                    tidalMonthlyDataList.sort(Comparator.comparing(TidalMonthlyData::getDate));
                    integration.setTideDatas(tidalMonthlyDataList);
                    integrations.add(integration);
                }
                integrations.sort(Comparator.comparing(Integration::getMonth));
                integrationResult.setIntegrations(integrations);
                String result = JSON.toJSONString(integrationResult);
                //
                BDMS_FBC_RESULT_P bdms_fbc_result_p = new BDMS_FBC_RESULT_P();
                bdms_fbc_result_p.setCschemeid(bdmsForecastparametersB.getPschemeid());
                bdms_fbc_result_p.setPtkey(stationCode);
                bdms_fbc_result_p.setDttp(10);
                bdms_fbc_result_p.setDtvalues(result);
                bdms_fbc_result_pMapper.deleteById(bdmsForecastparametersB.getPschemeid(),stationCode,"10");
                bdms_fbc_result_pMapper.insert(bdms_fbc_result_p);
                jsonResult.setRemark("调和分析计算成功");
                jsonResult.setData(integrationResult);
                jsonResult.setStatusCode(200);
                jsonResult.setSucceeded(true);
            }
            else {
                jsonResult.setStatusCode(201);
                jsonResult.setRemark("数据不全，请补齐数据");
                jsonResult.setSucceeded(true);
                jsonResult.setData("调和分析计算失败");
            }
        }else {
            jsonResult.setStatusCode(201);
            jsonResult.setRemark("该站点已存在相同时间点调和分析数据，请替换数据应用时间");
            jsonResult.setSucceeded(true);
            jsonResult.setData("调和分析计算失败");
        }



        return jsonResult;
    }





}